package com.yzcrm.erp.psi.service;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import com.yzcrm.erp.crm.common.CrmEnum;
import com.yzcrm.utils.R;

import java.util.ArrayList;
import java.util.List;

/**
 * @author jiang.xue
 */
public class PsiReportService {

    /**
     *
     * @param startDate
     * @param endDate
     * @param productName
     * @param productNameEn
     * @param productSpecName
     * @param storehouseId
     * @param customerName
     * @param productUpcCode
     * @param labels
     * @param elsNum
     * @param page
     * @param limit
     * @param priceType purchaser采购 director总监 salesman销售
     * @return
     */
    public R queryElsReport(
            String startDate,
            String endDate,
            String productName,
            String productNameEn,
            String productSpecName,
            String storehouseId,
            String customerName,
            String productUpcCode,
            String labels,
            String elsNum,
            Integer page,
            Integer limit,
            String sortColumns,
            String priceType) {

        Kv kv = Kv.by("startDate", startDate).set("endDate", endDate).set("productName", productName)
                .set("productNameEn", productNameEn).set("productSpecName", productSpecName)
                .set("storehouseId", storehouseId).set("customerName", customerName).set("productUpcCode", productUpcCode)
                .set("labels", labels).set("elsNum", elsNum);
        if(ObjectUtil.isNotNull(page) && ObjectUtil.isNotNull(limit)) {
            kv.set("page", (page - 1) * limit).set("limit", limit);
        }
        kv.set("priceType", priceType);
        kv.set("sortColumns", sortColumns); // order by els.els_id, els.label, els.els_num, s.name, elsd.product_spec_name


        // 根据权限不同获取不同价格
        List<Record> records = Db.find(Db.getSqlPara("psi.enterLeaveStockReport.elsReport", kv));
        records.forEach(record -> {

            // label名字 如：销售出库
            Integer label = record.getInt("label");
            record.set("label_name", CrmEnum.parse(label).getRemarks());

            Integer productUpcCodeUniqueFlag = record.getInt("product_upc_code_unique_flag");
            record.set("product_upc_code_unique_flag", Integer.valueOf(1).equals(productUpcCodeUniqueFlag) ? "是":"否");
        });

        SqlPara countSql = Db.getSqlPara("psi.enterLeaveStockReport.elsReportCount", kv);
        Integer count = Db.queryInt(countSql.getSql(), countSql.getPara());


        JSONObject resultJsonObject = new JSONObject();
        resultJsonObject.put("list", records);
        resultJsonObject.put("totalRow", count);
        return R.ok().put("data", resultJsonObject);
    }

    /**
     * 检索在库表头信息
     * @return
     */
    public List<Record> queryElsReportListHead(String priceType) {

        List<Record> listHead = new ArrayList<>();
//        listHead.add(new Record().set("name","").set("fieldName","els_id"));
        listHead.add(new Record().set("name","出入库类型").set("fieldName","labelName").set("columnName","label")); // 名字
        listHead.add(new Record().set("name","出入库单号").set("fieldName","elsNum").set("columnName","els_num"));
        listHead.add(new Record().set("name","仓库").set("fieldName","storehouseName").set("columnName","storehouse_name"));
        listHead.add(new Record().set("name","产品规格").set("fieldName","productSpecName").set("columnName","product_spec_name"));
        listHead.add(new Record().set("name","产品名称").set("fieldName","productName").set("columnName","product_name"));
        listHead.add(new Record().set("name","英文名称").set("fieldName","productNameEn").set("columnName","product_name_en"));
        listHead.add(new Record().set("name","单位").set("fieldName","productUnit").set("columnName","product_unit"));
        listHead.add(new Record().set("name","出入库数量").set("fieldName","elsQuantity").set("columnName","els_quantity"));
        listHead.add(new Record().set("name","是否有序列号").set("fieldName","productUpcCodeUniqueFlag").set("columnName","product_upc_code_unique_flag")); // 是否
        listHead.add(new Record().set("name","序列号").set("fieldName","productUpcCode").set("columnName","product_upc_code"));
        if("purchaser".equals(priceType)) {

            listHead.add(new Record().set("name","成本单价（未税）").set("fieldName","costPriceNoTax").set("columnName","cost_price_no_tax"));
            listHead.add(new Record().set("name","成本单价（含税）").set("fieldName","costPriceTax").set("columnName","cost_price_tax"));
            listHead.add(new Record().set("name","结算单价 （未税）").set("fieldName","settlementPriceNoTax").set("columnName","settlement_price_no_tax"));
            listHead.add(new Record().set("name","结算单价（含税）").set("fieldName","settlementPriceTax").set("columnName","settlement_price_tax"));
            listHead.add(new Record().set("name","总监权限单价（含税）").set("fieldName","directorPriceTax").set("columnName","director_price_tax"));
            listHead.add(new Record().set("name","销售表价（含税）").set("fieldName","salesPriceTax").set("columnName","sales_price_tax"));

        } else if("director".equals(priceType)) {

            listHead.add(new Record().set("name","结算单价 （未税）").set("fieldName","settlementPriceNoTax").set("columnName","settlement_price_no_tax"));
            listHead.add(new Record().set("name","结算单价（含税）").set("fieldName","settlementPriceTax").set("columnName","settlement_price_tax"));
            listHead.add(new Record().set("name","总监权限单价（含税）").set("fieldName","directorPriceTax").set("columnName","director_price_tax"));
            listHead.add(new Record().set("name","销售表价（含税）").set("fieldName","salesPriceTax").set("columnName","sales_price_tax"));

        } else if("salesman".equals(priceType)) {

            listHead.add(new Record().set("name","销售表价（含税）").set("fieldName","salesPriceTax").set("columnName","sales_price_tax"));
        }
        listHead.add(new Record().set("name","使用人（客户名称）").set("fieldName","contractCustomerName").set("columnName","contract_customer_name"));
        listHead.add(new Record().set("name","备注").set("fieldName","remarks").set("columnName","remarks"));

        return listHead;
    }
}
